<?php

class Email
{
	private $error;
	public function __construct()
	{}

	public function get_contacts($account, $password)
	{
		$account = current(explode('@', $account));
		$account = strtolower(trim($account));
		$password = trim($password);
		if (empty($account) || empty($password)) {
			$this->error = 'err_01';
			return false;
		}

		$tmp_dir = PATH_SEPARATOR == ':' ? '/dev/shm/invite/' : 'c:/windows/temp/';
		$tmp_dir = $tmp_dir . substr(md5(microtime(true) . rand(0, 999999999)), 0, 2);

		$cookie_jar1 = tempnam($tmp_dir, 'cookie1');

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'http://ssl.eyou.com/mail_login.php');
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, 'LoginName=' . $account . '&Password=' . $password);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		$return = curl_exec($ch);
		curl_close($ch);
		
		if(!preg_match("/location\.replace\('http:\/\/(.*?)\.eyou\.com\/'\);/", $return, $m)) {
			$this->error = 'err_01';
			return false;
		}

//		if (!preg_match("/location\.replace\('http:\/\/(.*?)\.eyou\.com\/folder_manager\.php'\);/", $return, $m)) {
//			$this->error = 'err_01';
//			return false;
//		}

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'http://' . $m[1] . '.eyou.com/personaddr.php');
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		$return = curl_exec($ch);
		curl_close($ch);

		preg_match_all('/<td class="nickname">\s+(.*?)\s+<\/td/', $return, $m1);
		preg_match_all('/<td id="mailAdd_(.*?)">/', $return, $m2);
		if (!count($m1[1]) || count($m1[1]) != count($m2[1])) {
			$this->error = 'err_02';
			return false;
		}

		$contacts = array();
		foreach ($m1[1] as $k => $v) {
			$tmp = array();
			$tmp['email'] = iconv('UTF-8', 'GBK', $m2[1][$k]);
			$tmp['name'] = iconv('UTF-8', 'GBK', $v);
			array_push($contacts, $tmp);
		}
		if (!count($contacts)) {
			$this->error = 'err_02';
			return false;
		}
		return $contacts;
	}

	public function get_error()
	{
		return $this->error;
	}
}

?>